package com.excilys.formation.project.webservices;

import static com.excilys.formation.project.dtos.VirementDTO.newVirementDTOBuilder;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.jws.WebService;

import org.joda.time.YearMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.converter.Converter;

import com.excilys.formation.project.dtos.ClientDTO;
import com.excilys.formation.project.dtos.CompteDTO;
import com.excilys.formation.project.dtos.OperationDTO;
import com.excilys.formation.project.dtos.VirementDTO;
import com.excilys.formation.project.entities.Client;
import com.excilys.formation.project.entities.Compte;
import com.excilys.formation.project.entities.Operation;
import com.excilys.formation.project.services.ClientService;
import com.excilys.formation.project.services.CompteService;
import com.excilys.formation.project.services.OperationService;
import com.excilys.formation.project.services.TransactionService;

@WebService(endpointInterface = "com.excilys.formation.project.webservices.IWebService")
public class WebServiceImpl implements IWebService {

	@Autowired
	private ClientService clientservice;

	@Autowired
	private CompteService compteservice;

	@Autowired
	private OperationService operationService;

	@Autowired
	private TransactionService transactionService;
	
	@Autowired
	@Qualifier("operation2OperationDTOConverter")
	private Converter<Operation, OperationDTO> operation2OperationDTOConverter;

	@Override
	public ClientDTO getClientByLogin(String login) {

		ClientDTO cDTO = new ClientDTO();
		Client realClient = clientservice.getClient(login);
		cDTO.setLogin(realClient.getLogin());
		cDTO.setNom(realClient.getNom());
		cDTO.setPrenom(realClient.getPrenom());

		List<Compte> tempList = new LinkedList<Compte>(realClient.getComptes());
		List<CompteDTO> tempListDTO = new LinkedList<CompteDTO>();

		for (Compte compte : tempList) {

			CompteDTO compteDTO = new CompteDTO();
			compteDTO.setNumero(compte.getNumero());
			compteDTO.setSolde(compte.getSolde());
			compteDTO.setTypeCompte(compte.getTypeCompte());

			tempListDTO.add(compteDTO);

		}

		cDTO.setComptes(tempListDTO);
		return cDTO;

	}

	@Override
	public List<CompteDTO> getListeComptes(String login) {

		ClientDTO connectedClient = getClientByLogin(login);
		return connectedClient.getComptes();
	}

	@Override
	public List<OperationDTO> getListeOperationByCompte(String numero) {

		
		List<Operation> listeOperation = new LinkedList<Operation>(operationService.getOperationsByCompteByYearMonth(numero, YearMonth.now()));
		List<OperationDTO> listeOperationDTO = new ArrayList<OperationDTO>(listeOperation.size());
		
		for (Operation operation : listeOperation) {
			listeOperationDTO.add(operation2OperationDTOConverter.convert(operation));
		}
		return listeOperationDTO;
	}

	@Override
	public VirementDTO nouveauVirement(String numeroEmetteur, String numeroRecepteur, double montant) {

		Compte compteEmetteur = compteservice.getCompte(numeroEmetteur);
		Compte compteRecepteur = compteservice.getCompte(numeroRecepteur);

		transactionService.creerVirement(compteEmetteur, compteRecepteur, montant);

		return newVirementDTOBuilder().withNumeroEmetteur(numeroEmetteur).withNumeroRecepteur(numeroRecepteur).withMontant(montant).build();
	}
}
